//
//  LeftTableView.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/23.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "LeftTableView.h"
@interface LeftTableView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSString  *typeStr;//类型
@property (nonatomic,strong)NSMutableArray *chooseArr;
@property (nonatomic, strong)NSArray *firstArr;//主数据
@property (nonatomic, strong)NSArray *secondArr;//备用 万一用到了呢
@property (nonatomic, strong) UITableView *table;
@property (nonatomic, assign) NSInteger selectRow;//选中的Row
@property (nonatomic,strong)NSString *oldChooseStr;//上一次的选择
@property (nonatomic,assign)BOOL isFirst;
@end

@implementation LeftTableView

- (void)WithFrame:(CGRect)frame KindStr:(NSString *)kindStr DataArr:(NSArray *)dataArr SecondArr:(NSArray *)dataArr2 ChooseArr:(NSMutableArray *)chooseArr
{
    self.backgroundColor = [UIColor whiteColor];
    _firstArr = dataArr;
    _secondArr = dataArr2;
    _typeStr = kindStr;
    _selectRow = 0;
    _chooseArr = [[NSMutableArray alloc]initWithArray:chooseArr];
    [self addSubview:self.table];
}
-(UITableView *)table
{
    if (!_table) {
        _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width,self.frame.size.height) style:UITableViewStyleGrouped];
        _table.delegate = self;
        _table.dataSource = self;
        _table.showsHorizontalScrollIndicator = NO;
        _table.showsVerticalScrollIndicator = NO;
        _table.separatorStyle = 0;
        _table.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        _table.backgroundColor = [UIColor whiteColor];
    }
    return _table;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0000001;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _firstArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40*screenW_6;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"Cell%ld",indexPath.row]];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"Cell%ld",indexPath.row]];
    }
    //加载cell
    cell.backgroundColor = [UIColor colorWithHexString:@"#EEEEEE"];
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor whiteColor];
//    改字体的颜色
    cell.textLabel.highlightedTextColor = greenColor;
    [cell.textLabel setTextColor:TextBlack];//设置cell的字体的颜色
    cell.textLabel.text = _firstArr[indexPath.row];
    cell.textLabel.textAlignment = 1;
    cell.textLabel.font = [UIFont systemFontOfSize:14*screenW_6];

    if ([_chooseArr containsObject:cell.textLabel.text]) {
        _isFirst = YES;
        _oldChooseStr = cell.textLabel.text;
        [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
    }
    //第一次没有做任何选择
    if (indexPath.row == _secondArr.count - 1  && _isFirst == NO) {
       [tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionMiddle];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _selectRow = indexPath.row;
    if (_chooseRowBlock) {
        [_chooseArr removeObject:_oldChooseStr];
        [_chooseArr addObject:_firstArr[indexPath.row]];
        self.chooseRowBlock(_selectRow,_chooseArr);
    }
}
-(void)chooseSelect
{
    if ([self.table.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {
        [self.table.delegate tableView:self.table didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    }
}
@end
